package com.mvvm.demo.ui.login

import com.mvvm.base.base.BaseResult
import com.mvvm.base.base.repository.BaseRepositoryBoth
import com.mvvm.base.base.repository.ILocalDataSource
import com.mvvm.base.base.repository.IRemoteDataSource
import com.mvvm.base.util.MD5
import com.mvvm.demo.db.UserInfoDao
import com.mvvm.demo.entity.UserInfo
import com.mvvm.demo.http.service.ServiceManager
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import org.json.JSONObject
import java.text.SimpleDateFormat
import java.util.*

class LoginRepository(
        remoteDataSource: LoginRemoteDataSource,
        localDataSource: LoginLocalDataSource
) :
        BaseRepositoryBoth<LoginRemoteDataSource, LoginLocalDataSource>(
                remoteDataSource,
                localDataSource
        ) {

    suspend fun login(phone: String, code: String): BaseResult<UserInfo> {
        var data = JSONObject()
        data.put("phone", phone)
        data.put("verificationCode", code)
        val body = getBaseParams(data)
        var date = Date()
        val format = SimpleDateFormat("yyyyMMddHHmm")
        var reqDate = format.format(date)
        var sign = MD5("jiaomigo.gialen.com#2019|" + phone + "|" + reqDate + "|" + "app/login")
        val userInfo = remoteDataSource.login(sign, reqDate, body)
        return userInfo
    }

    suspend fun getVerficationCode(phone: String): BaseResult<JSONObject> {
        var data = JSONObject()
        data.put("phone", phone)
        val body = getBaseParams(data)
        var date = Date()
        val format = SimpleDateFormat("yyyyMMddHHmm")
        var reqDate = format.format(date)
        var sign = MD5("jiaomigo.gialen.com#2019|" + "" + "|" + reqDate + "|" + "app/req/user.getLoginRegistryVerification")
        val result = remoteDataSource.getVerficationCode(sign, reqDate, body)
        return result
    }
    suspend fun insertUser(userInfo: UserInfo) {
        localDataSource.insertUser(userInfo)
    }
    suspend fun queryUserInfo():UserInfo?{
        return localDataSource.queryUserInfo()
    }
    suspend fun deleteUserInfoAll(){
        localDataSource.deleteUserInfoAll()
    }

}

class LoginRemoteDataSource(
        private val serviceManager: ServiceManager
) : IRemoteDataSource {
    suspend fun getVerficationCode(
            sign: String,
            reqDate: String,
            requestBody: RequestBody
    ): BaseResult<JSONObject> {
        return serviceManager.loginService.getVerficationCode(sign, reqDate, requestBody)
    }

    suspend fun login(
            sign: String,
            reqDate: String,
            requestBody: RequestBody
    ): BaseResult<UserInfo> {
        return serviceManager.loginService.login(sign, reqDate, requestBody)
    }
}

class LoginLocalDataSource(
        private val userInfoDao: UserInfoDao
) : ILocalDataSource {
    suspend fun insertUser(userInfo: UserInfo) {
        userInfoDao.insertUserInfo(userInfo)
    }
    suspend fun queryUserInfo():UserInfo?{
       return userInfoDao.queryUserInfo()
    }
    suspend fun deleteUserInfoAll(){
        userInfoDao.deleteUserInfoAll()
    }



}
fun getBaseParams(data: JSONObject?): RequestBody {
    return RequestBody.create("application/json".toMediaTypeOrNull(), data.toString())

}